/*!
 @file MujiMediaLoadRequest.h
 @framework ECCoreLibrary

 @author Created by Paul Cantrell on 6/11/14.
 @copyright Copyright (c) 2014 EnglishCentral, Inc. All rights reserved.

 @internal
 */


#import <Foundation/Foundation.h>
#import <QuartzCore/CoreAnimation.h>
#import <AVFoundation/AVFoundation.h>

@class MujiMediaLoadRequest;

@protocol MujiMediaClientProtocol <NSObject>

- (void)downloadFailed:(MujiMediaLoadRequest *)mediaLoadRequest;
- (void)downloadCompleted:(MujiMediaLoadRequest *)mediaLoadRequest;

@end

/*!
 @protocol MujiMediaLoadRequestProtocol
 */
@protocol MujiMediaLoadRequestProtocol <NSObject>

- (void)prepareToPlayAsset:(AVURLAsset *)asset withKeys:(NSArray *)requestedKeys;

@end

@protocol MujiMediaLoadRequestDelegate <NSObject>

- (void)newDataHasArrived;
- (void)downloadHasBegun;
- (void)downloadHasCompleted;
- (void)downloadFailed:(MujiMediaLoadRequest *)mediaLoadRequest withError:(NSError *)error;

@end

/*!
 @class MujiMediaLoadRequest
 
 @discussion   This class is instantiated for each download request. It takes care of performing the download
 and calling back delegates as new data arrives. There are two typical delegates - the
 MediaLoadRequestManager who needs to know when downloads finish so that it can update it's
 bill of materials about cached movies, and also the class such as HTTPAsyncForwardedRequestResponse
 which is returning chunks of data to the media player. This class is also serializable so that
 the MediaLoadRequestManager can store it as part of the bill of materials, thus this object
 fully describes the state of the target url - whether it has been downloaded, how big it is,
 and the name of the local movie file if it has been downloaded.
 */
@interface MujiMediaLoadRequest : NSObject

@property (nonatomic, assign) UInt64 contentLength;
@property (nonatomic, strong) NSHTTPURLResponse *response;
@property (nonatomic,assign) BOOL downloadHasFinished;
@property (readwrite, nonatomic, assign, getter=isDownloadInProgress) BOOL downloadInProgress;
@property (nonatomic,weak) id < MujiMediaClientProtocol, NSObject > mediaClientDelegate;
@property (nonatomic, copy) NSError *error;

@property (nonatomic, copy) dispatch_queue_t clientAccessQueue;
@property (nonatomic,strong) AVURLAsset *avAsset;

- (void)loadAssetWithDelegate:(id<MujiMediaLoadRequestProtocol>)delegate;

- (id)initWithUrlString:(NSString *)urlString;
- (void)setDate:(NSDate *)date;
- (void)addAsDelegate:(id<MujiMediaLoadRequestDelegate>)delegate;
- (void)removeAsDelegate:(id<MujiMediaLoadRequestDelegate>)delegate;
- (UInt64)availableContentLength;
- (NSString *)encodedUrlString;
- (NSString *)originalUrlString;
- (NSString *)localFilename;
- (NSString *)localFilePath;
- (BOOL)localFileIsPresent;
- (void)uncache;
- (BOOL)cancelDownload;

// Methods below are used only by subclasses

- (BOOL)createLocalMovieFile;
- (NSArray *)delegates;
- (void)signalDownloadHasBegun;
- (void)signalNewDataHasArrived;
- (void)signalDownloadHasFailedWithError:(NSError *)error;

@end
